package com.oil.petro.eshop.utils;

import com.oil.petro.eshop.bean.CartBean;
import com.oil.petro.eshop.bean.ProductBean;
import com.oil.petro.eshop.entity.Order;
import com.oil.petro.eshop.entity.Category;
import com.oil.petro.eshop.entity.Manufacturer;
import com.oil.petro.eshop.entity.Product;
import com.oil.petro.eshop.service.CategoryService;
import com.oil.petro.eshop.service.ManufacturerService;
import com.oil.petro.eshop.service.ProductService;
import com.oil.petro.eshop.service.UserAccountService;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class ProductHelper {

    public Product prepareNewProdcutToSave(ProductBean productBean, CategoryService categoryService, ManufacturerService manufacturerService) {
        Product product = new Product();
        product.setName(productBean.getName());
        product.setDescription(productBean.getDescription());
        product.setCost(Float.parseFloat(productBean.getPrice()));
        product.setAmount(Integer.parseInt(productBean.getAmount()));

        int cId = categoryService.getCategoryIdByName(productBean.getCategoryName());
        int mId = manufacturerService.getManufacturerIdByName(productBean.getManufacturerName());

        product.setCategoryId(cId);
        product.setManufacturerId(mId);
        productBean.getManufacturerName();
        String image = ImageHelper.setImage(productBean.getImage().getBytes());
        System.out.println("dlogosc " + image.length());
        product.setImage(image);
        product.setOrderStatus(OrderStatusUtil.NEW);
        //do zmiany
        // product.setCartId(1);

        return product;
    }

    public Product prepareProdcutToUpdate(ProductBean productBean, CategoryService categoryService, ManufacturerService manufacturerService, ProductService productService) {
        Product product = productService.getProduct(productBean.getId());
        product.setName(productBean.getName());
        product.setDescription(productBean.getDescription());
        product.setCost(Float.parseFloat(productBean.getPrice()));
        product.setAmount(Integer.parseInt(productBean.getAmount()));

        int cId = categoryService.getCategoryIdByName(productBean.getCategoryName());
        int mId = manufacturerService.getManufacturerIdByName(productBean.getManufacturerName());

        product.setCategoryId(cId);
        product.setManufacturerId(mId);
        productBean.getManufacturerName();
        String image;
        if(productBean.getImage().getSize() != 0L) {
            image = ImageHelper.setImage(productBean.getImage().getBytes());
            product.setImage(image);
        }
        product.setOrderStatus(OrderStatusUtil.NEW);

        return product;
    }

    public ProductBean prepareProductToDisplayDetails(Product product, CategoryService categoryService, ManufacturerService manufacturerService) {
        ProductBean productBean = new ProductBean();
        productBean.setDescription(product.getDescription());
        productBean.setPrice(getDisplayedPrice(product.getCost()).replaceAll(",", "."));
        productBean.setName(product.getName());
        productBean.setAmount(product.getAmount() + "");
        productBean.setFile(product.getImage());
        Category category = categoryService.getCategory(product.getCategoryId());
        Manufacturer manufacturer = manufacturerService.getManufacturer(product.getManufacturerId());
        productBean.setCategoryName(category.getName());
        productBean.setManufacturerName(manufacturer.getName());
        return productBean;
    }

    public List<Product> changeLengthProductDescription(List<Product> list) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getDescription().length() > 180) {
                list.get(i).setDescription(list.get(i).getDescription().substring(0, 180).concat("..."));
            }
        }
        return list;
    }

    public List<ProductBean> getProductListForEditDelete(List<Product> productList) {

        List<ProductBean> list = new ArrayList<>();

        changeLengthProductDescription(productList);

        for (Product p : productList) {

            list.add(new ProductBean(
                    OrderStatusUtil.getOrderStatusTooltip(p.getOrderStatus()),
                    OrderStatusUtil.getOrderStatusIcon(p.getOrderStatus()), p));

        }
        return list;

    }

    public static String getDisplayedPrice(float price) {
        return String.format("%.2f", price);
    }

}
